package api;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.Blog;
import dao.BlogDao;
import dao.User;
import dao.UserDao;

import javax.jws.soap.SOAPBinding;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
//对象都空了，还要返回一个空的json是为什么
/* user->null和user=new User()不一样的，user=null，最终返回的数据是一个null，但是，这也就会导致ajax会回调方法这里的解析出错，
因为咱们返回中响应设置的content type为application/json，ajax这里也就预期返回一个json，返回一个null这样一个字符串，ajax解析就会出问题
使用new User做法，返回的响应{userId:0,username:"}这个情况也是合法的json，ajax能够正确解析
 */

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    private ObjectMapper objectMapper=new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String blogId= req.getParameter("blogId");
        if(blogId==null){
            //博客列表页
            //从session中拿到user对象.
            HttpSession session=req.getSession(false);
            if (session==null){
                User user=new User();
                String respJson= objectMapper.writeValueAsString(user);
                resp.setContentType("application/json;charset=utf8");
                resp.getWriter().write(respJson);
                return;
            }
            User user=(User) session.getAttribute("user");
            if (user==null){
                user=new User();
                String respJson=objectMapper.writeValueAsString(user);
                resp.setContentType("application/json;charset=utf8");
                resp.getWriter().write(respJson);
                return;
            }
            String respJson=objectMapper.writeValueAsString(user);
            resp.setContentType("application/json;charset=utf8");
            resp.getWriter().write(respJson);
        }
        else{
            //博客详情页
            //查询数据库
            BlogDao blogDao=new BlogDao();
            Blog blog= blogDao.getBlog(Integer.parseInt(blogId));
            if (blog==null){
                User user=new User();
                String respJson=objectMapper.writeValueAsString(user);
                resp.setContentType("application/json;charset=utf8");
                resp.getWriter().write(respJson);
                return;
            }
            UserDao userDao=new UserDao();
            User user=userDao.getUserById(blog.getUserId());
            if (user==null){
                user=new User();
                String respJson=objectMapper.writeValueAsString(user);
                resp.setContentType("application/json;charset=utf8");
                resp.getWriter().write(respJson);
                return;
            }
            String  respJson=objectMapper.writeValueAsString(user);
            resp.setContentType("application/json;charset=utf8");
            resp.getWriter().write(respJson);
        }
    }
}
